<?php

namespace CNEPAdmin\Controller;

use CNEPAdmin\Controller\CommonController;

class MaterialController extends CommonController {

    protected $_modelName = "Material";

    protected function setCode() {
        $this->_code = "System"; // 属于系统管理模块
    }

    public function index() {
		$this->checkPriv('material', 1, false); // 权限
		$this->assign('imgDomain',C('IMG_DOMAIN'));
		$this->_getOnePageData("getMaterialList", "getMaterialListLength");
    }
	
	public function view() {
		$id = intval($_GET['id']);
		$model = M('Material')->where(array('materialID'=>$id))->find();
		$this->assign('content',$model['content']);
		$this->display();
	}

    /**
     * 得到条件查询数组
     * @return Array
     */
    protected function _getQueryArr(&$where, &$order) {

        if (strlen(trim(I("sTitle"))) > 0) {
            $where['title'] = array('like', "%" . trim(I("sTitle", '')) . '%');
        }

        $where['isOn'] = 1;

        $orderBy = 'addTime';
        $desc = 'desc';
        if (intval(I("sDesc")) === 1) {
            $desc = 'asc';
        }
        if (strlen(trim(I("sOrderBy", ''))) > 0) {
            $orderBy = I("sOrderBy");
        }

        $order = array(
            $orderBy => $desc
        );
		//dump($where);
        return $where;
    }

    /**
     *  添加
     */
    private function _addMaterial() {
        $this->_setModel();
        $rt = $this->_model->addMaterial($_POST);
        return $rt;
    }
	
	public function edit() {
        $this->checkPriv('material', 1, false);
        $rt = true;
        $id = intval(I("materialID", 0));

        if ($id > 0) { // 编辑
            $arr = D("Material")->getMaterial($id);
            if ($arr) {
            	!empty($arr['img'])?$arr['img']=C("IMG_DOMAIN").$arr['img']:"";
                $arr['addtime'] = date("Y/m/d H:i:s", $arr['addtime']);
                unset($arr['isOn']);
            }
        } else {
            $error = "无效ID";
            $rt = false;
        }

        $this->ajaxReturn(array('result' => $rt, 'desc' => $error, 'arr' => $arr));
    }
	
	public function saveMaterial() {
        $this->checkPriv("material", 2);
        $materialID = intval(I("post.materialID"));
        $adminArr = $_SESSION['adminArr'];

        if ($adminArr) {
            !empty($_POST['img'])?$_POST['img']=$this->get_url_path(I('post.img')):"";
            
            D()->startTrans();

            if ($materialID > 0) {
                $rt = $this->_updateMaterial($materialID);
            } else {
                $rt1 = $this->_addMaterial();
				$materialID = intval($rt1);
				$_POST['materialID'] = $materialID;
				$_POST['url'] = C('SERVER_DOMAIN').'/CNEPShop/Material/view.html?id=' . $materialID;
				$rt = $this->_updateMaterial($materialID);
            }

            if ($rt !== false && $rt1 !== false)
                D()->commit();
            else
                D()->rollback();

            $this->ajaxReturn(array(
                'result'=>$rt,
                'desc' => $this->_error
            ));
        } else {
            $this->_error = "登陆失效，请重新登录";
            $this->_myAjaxReturn(false);
        }
    }

    /**
     * 删除
     */
    public function deleteMaterial() {
        $this->_ajaxOperate( 'deleteMaterial','material' , 2, I("post.materialID"));
    }

    

    /**
     * 更新基本信息(需要更新什么就提交什么)
     */
    private function _updateMaterial($materialID) {

        $rt = FALSE;

        if ($materialID > 0) {
            // 判断是否已删除
            $model = D("Material");
            $arr = $model->where("materialID=$materialID")->find(array('isOn'));
            if (intval($arr['isOn']) === 1) {
                
                $rt = $model->updateMaterial($_POST);
               
            } else {
                $this->_error = "素材不存在";
            }
        } else {
            $this->_error = "无效ID";
        }
        return $rt;
    }

}
